<?php


class userActions extends sfActions 
{
   public function executeCompte(sfWebRequest $request)
    {
       $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin'); 
       $this->form = new $class();
       $this->formInscription = new InscriptionForm(); 
       if (!$request->ismethod('post'))
       {
            $referer = $this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer();
            $this->getUser()->setAttribute('referer', $referer);
       }
       
      
       if(!$this->getUser()->isAuthenticated())
       {
            if ($request->ismethod('post'))
            {
                if ($request->getParameter('send') == "signin")
                {
                        $this->form->bind($request->getParameter('signin'));

                        if ($this->form->isValid())
                        {
                            $values = $this->form->getValues(); 
                            $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);
                           
                            //$this->forward('user', 'panel');
                            $this->redirect($this->getUser()->getAttribute('referer'));
                        }
                } 
                
                if ($request->getParameter('send') == "inscription")
                {
                     $this->formInscription->bind($request->getParameter('signup'));
                     if ($this->formInscription->isValid())
                     {
                         $values = $this->formInscription->getValues();
                        
                         
                    $user= new sfGuardUser();
                    $user->setEmailAddress($values['email_address']);
                    $user->setUsername($values['username']);
                    $user->setLastName($values['last_name']);
                    $user->setFirstName($values['first_name']);
                    $user->setPassword($values['password']);
                    $user->setIsActive(1);
                    $user->getProfile()->setZipcode($values['zipcode']);
                    $user->getProfile()->setBirthdaydate($values['birthdaydate']);
                    $user->getProfile()->setCity($values['city']);
                    $user->getProfile()->setGenre($values['gender']);
                        $user->addGroupByName("member");
                    $user->save();
                                        $this->getUser()->setAttribute('email_address',$values['email_address']);

                    $this->setTemplate('inscription');
                     }
                }
            }
       }
       else
       {
           
                            
           $this->forward('user', 'panel');
       }
       
        
    }
    public function executePanel(sfWebRequest $request){
        
    }
    
    public function executeSaveRating(sfWebRequest $request){
        if ($request->isXmlHttpRequest()){
                  //echo $request->getParameter('action');exit;     

            if ($request->getParameter('myaction') == 'rating'){
                switch ($request->getParameter('idBox')) {
                    case 1:
                        $this->getUser()->setAttribute('prix',$request->getParameter('rate'));
                         $message = $request->getParameter('rate');
                               return $this->renderText($message);

                        break;
                    case 2:
                        $this->getUser()->setAttribute('ambiance',$request->getParameter('rate')); 
                        $message = $request->getParameter('rate');
                               return $this->renderText($message);
                        break;
                    case 3:
                        $this->getUser()->setAttribute('service',$request->getParameter('rate'));
                        $message = $request->getParameter('rate');
                               return $this->renderText($message);
                        break;
                    case 4:
                        $this->getUser()->setAttribute('nourriture',$request->getParameter('rate'));
                        $message = $request->getParameter('rate');
                               return $this->renderText($message);
                        break;
                    default:
                        $this->getUser()->setAttribute('prix',0);
                        $this->getUser()->setAttribute('ambiance',0);
                        $this->getUser()->setAttribute('service',0);
                        $this->getUser()->setAttribute('nourriture',0);
                        break;
                }
                                          
           }
            }
        }
   
    public function executePostComment(sfWebRequest $request){
    
        if ($request->isXmlHttpRequest()){
            $message = $request->getParameter('myParam');
            //$rate = $request->getParameter('rate');
            $ambiance = $this->getUser()->getAttribute('ambiance');
            $nourriture = $this->getUser()->getAttribute('nourriture');
            $prix = $this->getUser()->getAttribute('prix');
            $service = $this->getUser()->getAttribute('service');
            
            $alreadyComment = Doctrine_Core::getTable('HalalmenuRestaurantCommentsByUser')->getCountCommentsByRestaurantUser($this->getUser()->getGuardUser()->getProfile()->getId(),$this->getUser()->getAttribute('idrestaurant'));
           
            if (strlen($message) > 0){
                if ($alreadyComment == 0){
                    if (empty($message) | empty($ambiance) | empty($service) | empty($prix) | empty($nourriture)){
                        $message = "Saisissez votre commentaire et notez le restaurant";
                    }
                    else{
                            $commentaire = new HalalmenuRestaurantCommentsByUser();
                            $commentaire->setComment($message);
                            $commentaire->setTitle("Test Saidi");
                            $commentaire->setRateAmbiance($this->getUser()->getAttribute('ambiance'));
                            $commentaire->setRateNourriture($this->getUser()->getAttribute('nourriture'));
                            $commentaire->setRatePrice($this->getUser()->getAttribute('prix'));
                            $commentaire->setRateService($this->getUser()->getAttribute('service'));
                            $commentaire->setIdrestaurant($this->getUser()->getAttribute('idrestaurant'));
                            $commentaire->setIduser($this->getUser()->getGuardUser()->getProfile()->getId());
                            $commentaire->setCreatedAt(date('Y-m-d'));
                            $commentaire->setUpdatedAt(date('Y-m-d'));
                            $commentaire->save();
                            $message = "<font color='green'>Merci de votre contibution, un modérateur validera votre commentaire sous les 24H</font>";
                        }
                }
                else{
                    $message = "Un commentaire de votre part a déja été posté pour ce restaurant.";
                }
            }
            else {
                $message = "Commentaire et notes sont requises, veuillez noter et commenter le restaurant";
            }
              return $this->renderText($message);

            }
        }
  
    public function executeInscription(){
            
  }
}
?>
